<%@page import="javax.persistence.Query"%>
<%@page import="multiface.web.util.Define"%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.util.List"%>
<%@page import="multiface.web.util.Util"%>
<%@page import="multiface.promonet.jpa.Lavoro"%>
<%@page import="multiface.promonet.jpa.Utente"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="multiface.promonet.jpa.util.PersistenceManager"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%
    EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
    EntityManager em = emf.createEntityManager();

    Utente u = (Utente) session.getAttribute(Define.USER_SESSION);
%>
<script>
    function go(idLav){
        window.location = "?page=report&where=new&lavoroid="+idLav;
    }
</script>
<div id="center">
    <table id="center-table" class="center-table">
        <tr style="background-color: #00A551;color:white;font-weight: bold;height: 24px;">
            <td>Data</td>
            <td>Punto Vendita</td>
            <td>Orario Promo</td>
            <td>Promo</td>
            <td style="width: 30px;"></td>
            <td style="width: 20px;"></td>
        </tr>
        <%

            //em.refresh(u);
            List<Lavoro> list = new LinkedList<Lavoro>();

            String where = request.getParameter("where");
            if (where == null) {
                where = "open";
            }
            if (where.equals("open")) {
                list = em.createNativeQuery("SELECT l.* FROM lavoro l, report r WHERE l.promoter=? AND l.report=r.idreport AND (r.stato='APERTO' OR r.stato='NUOVO') ORDER BY data", Lavoro.class).setParameter(1, u.getIdutente()).getResultList();
                
                //Query q = em.createQuery("SELECT l FROM lavoro l, report r WHERE l.promoter=:idpromoter AND l.report=r.idreport AND (r.stato='APERTO' OR r.stato='NUOVO') ORDER BY data", Lavoro.class);
                //q.setParameter("idpromoter", u.getIdutente());
                //list = q.getResultList();
            }
            if (where.equals("close")) {
                list = em.createNativeQuery("SELECT l.* FROM lavoro l, report r WHERE l.promoter=? AND l.report=r.idreport AND r.stato='CHIUSO' ORDER BY data", Lavoro.class).setParameter(1, u.getIdutente()).getResultList();
            }

            for (Lavoro l : list) {
                em.refresh(l);
        %>


        <tr>
            <td><%=Util.FormatData(l.getData(), "dd/MM/yyyy")%></td>
            <td><%=l.getPuntovendita().getNome()%> (<%=l.getPuntovendita().getIndirizzo()%> - <%=l.getPuntovendita().getCitta()%>)</td>
            <td><%=l.getOrario()%></td>
            <td><a href="?page=promo&promoid="><%=l.getPromozione().getNome()%></a></td>
            <%if (l.getReport().getStato().equals("CHIUSO")) {%>
            <td><input type="button" value="Richiedi Riapertura"/></td>
            <td></td>
            <%} else {%>
            <%if (l.getReport().getStato().equals("APERTO")) {%>
            <td><input type="button" onclick="javascript:go(<%=l.getIdlavoro()%>)"  value="Modifica Report"/></td>
            <td><input type="button" value="Invia Report"/></td>
                <%} else {%>
            <td><input type="button" onclick="javascript:go(<%=l.getIdlavoro()%>)" value="Compila Report"/></td>
            <td></td>
            <%}
                }%>
        </tr>
        <%  }
        %>
    </table>
</div>
<%em.close();%>